package testcase;

import java.io.File;

import java.io.IOException;
import java.util.ArrayList;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class TestCaseConverter {

	 
	private String tfsFile = "c:/TC.xls";
	private final int nameColumn = 1;
	private final int descriptionColumn = 2;
	private final int stepColumn = 4;
	private final int resultColumn = 5;
	private File excel;
	
	public static void main(String args[]){
		//Test Case Quelldatei
		//TestCaseConverter converter = new TestCaseConverter();
//		try {
//			converter.read();
//		} catch (RowsExceededException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (WriteException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

	}
	private Builder output;
	public TestCaseConverter(File excel){
		//Erstellen einer outputCreator Instanz
		//Result.xls ist das Template, das gefllt wird
		//und am Ende in die output.xls geschrieben wird
		this.excel = excel;
		output = new Builder(excel.getAbsolutePath());
		
		
	}
	
	public void read() throws IOException, RowsExceededException, WriteException  {
		//Deklarierung und Initialisierung von lokalen Variablen
		File inputWorkbook = excel;
		Workbook inputW;
		ArrayList<String> steps = new ArrayList<String>(); 
		ArrayList<String> results = new ArrayList<String>(); 
		String testName = null;
		String testDescription = null;

		try{
			inputW = Workbook.getWorkbook(inputWorkbook);
			//Auswahl des ersten Tabellenblatts des zu konvertierenden xls
			Sheet input = inputW.getSheet(0);
			for(int i = 1; i < input.getRows(); i++){
				//Testet ob in dieser Zeile ein Testfall steht
				//z.Zt. nicht mehr unbedingt notwendig
				Cell id = input.getCell(1, i);
				for(int j = 0; j < input.getColumns(); j++){
					Cell cell = input.getCell(j, i);
					//Auslesen der Spalten
					if(!id.getContents().equals("")){
						switch (j) {
						case nameColumn: testName = cell.getContents(); break;
						case descriptionColumn: testDescription = cell.getContents(); break;
						case stepColumn: steps.add(cell.getContents()); break;
						case resultColumn: results.add(cell.getContents()); break;
						default: break;
						}
					}
					else{
						if (j == stepColumn && !cell.getContents().equals(""))
							steps.add(cell.getContents());
						if (j == resultColumn && !cell.getContents().equals(""))
							results.add(cell.getContents());
						if (j == resultColumn && cell.getContents().equals("")){
							output.writeData(testName, testDescription, steps, results);
							testName = null;
							testDescription = null;
							steps.clear();
							results.clear();	
						}
					}
				}
			}
				
		}catch (BiffException e){
			e.printStackTrace();
		}
		//Schreiben der Daten per outputCreator in die output.xls
		output.writeData(testName, testDescription, steps, results);
	}
}
